package com.zyp.weibo_demo.controller;

import com.zyp.weibo_demo.mapper.WeiboMapper;
import com.zyp.weibo_demo.pojo.dto.WeiboDto;
import com.zyp.weibo_demo.pojo.entity.Weibo;
import com.zyp.weibo_demo.pojo.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/weibo/")
public class WeiboController {

    @Autowired
    WeiboMapper mapper;
    @RequestMapping("/insert")
    public int insert(@RequestBody WeiboDto weiboDto, HttpSession session) {
        UserVo u = (UserVo) session.getAttribute("user");
        if (u==null) {
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDto,weibo);
        weibo.setCreated(new Date());
        weibo.setUser_id(u.getId());
        mapper.insert(weibo);
        return 1;
    }

}
